#include "list.h"

static list_app_t *local_app;

extern lv_indev_t *my_indev;
extern app_t curr_page;
extern home_app_t home_app;
extern light_app_t light_app;
extern clock_app_t clock_app;
extern netconfig_app_t netconfig_app;

void remove_list_app()
{
}

static void event_handler(lv_event_t *event)
{
    lv_event_code_t code = lv_event_get_code(event);
    lv_obj_t *obj = lv_event_get_target(event);
    if (code == LV_EVENT_CLICKED)
    {
        // LV_LOG_USER("Clicked: %s", lv_list_get_btn_text(local_app->list1, obj));
        const char *btn_name = lv_list_get_btn_text(local_app->list1, obj);
        if (strcmp(btn_name, "Home") == 0)
        {
            remove_list_app();
            create_home_app(&home_app);
        }
        else if (strcmp(btn_name, "Set Light") == 0)
        {
            remove_list_app();
            create_light_app(&light_app);
        }
        else if (strcmp(btn_name, "Network Config") == 0)
        {
            remove_list_app();
            create_netconfig_app(&netconfig_app);
        }
        else if (strcmp(btn_name, "Clock") == 0)
        {
            remove_list_app();
            create_clock_app(&clock_app);
        }
    }
}

void create_list_app(list_app_t *app)
{
    local_app = app;
    curr_page = LIST_APP;

    lv_obj_t *scr = lv_obj_create(NULL);
    // 界面
    lv_group_t *g = lv_group_create();
    lv_group_set_default(g);
    lv_indev_set_group(my_indev, g);

    local_app->list1 = lv_list_create(scr);
    lv_obj_set_width(local_app->list1, lv_pct(100));
    lv_obj_set_height(local_app->list1, lv_pct(100));

    lv_obj_t *btn;

    lv_list_add_text(local_app->list1, "App");
    btn = lv_list_add_btn(local_app->list1, LV_SYMBOL_HOME, "Home");
    lv_obj_add_event_cb(btn, event_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(local_app->list1, LV_SYMBOL_BELL, "Clock");
    lv_obj_add_event_cb(btn, event_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(local_app->list1, LV_SYMBOL_CALL, "Network Config");
    lv_obj_add_event_cb(btn, event_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(local_app->list1, LV_SYMBOL_CHARGE, "Set Light");
    lv_obj_add_event_cb(btn, event_handler, LV_EVENT_CLICKED, NULL);

    lv_scr_load_anim(scr, LV_SCR_LOAD_ANIM_MOVE_TOP, 300, 0, true);
}